home *** CD-ROM | disk | FTP | other *** search
/ MPEG Toolkit / MPEG Toolkit.iso / os2 / mpegplay / gdith.c < prev    next >
C/C++ Source or Header  |  1997-01-01  |  13KB  |  609 lines

  1. /*
  2.  * Copyright (c) 1992 The Regents of the University of California.
  3.  * All rights reserved.
  4.  * 
  5.  * Permission to use, copy, modify, and distribute this software and its
  6.  * documentation for any purpose, without fee, and without written agreement is
  7.  * hereby granted, provided that the above copyright notice and the following
  8.  * two paragraphs appear in all copies of this software.
  9.  * 
  10.  * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
  11.  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
  12.  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  13.  * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14.  * 
  15.  * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  16.  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  17.  * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
  18.  * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
  19.  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  20.  */
  21. #include <math.h>
  22. #include "video.h"
  23. #include "proto.h"
  24. #include "dither.h"
  25.  
  26. /* Range values for lum, cr, cb. */
  27. int LUM_RANGE;
  28. int CR_RANGE;
  29. int CB_RANGE;
  30.  
  31. /* Array that remaps color numbers to actual pixel values used by X server. */
  32.  
  33. /* @@@ Under OS/2 we know that pixel[i]==i because we know they are
  34.    requested in order by mpeg_play, and we know that they are dished out
  35.    in order by the XSTUB module. Hence we have commented out use of this
  36.    array wherever it occurs and placed a comment pointing here. */
  37. unsigned char pixel[256];
  38.  
  39. /* Arrays holding quantized value ranged for lum, cr, and cb. */
  40.  
  41. int *lum_values;
  42. int *cr_values;
  43. int *cb_values;
  44.  
  45. /* Declaration of global variable containing dither type. */
  46.  
  47. extern int ditherType;
  48.  
  49. /* Structures used by the X server. */
  50.  
  51. Display *display;
  52.  
  53. static XImage *ximage = NULL;
  54. static Colormap cmap;
  55. static Window window;
  56. static GC gc;
  57.  
  58.  
  59.  
  60. /*
  61.  *--------------------------------------------------------------
  62.  *
  63.  * InitColor --
  64.  *
  65.  *    Initialized lum, cr, and cb quantized range value arrays.
  66.  *
  67.  * Results: 
  68.  *      None.
  69.  *
  70.  * Side effects:
  71.  *      None.
  72.  *
  73.  *--------------------------------------------------------------
  74.  */
  75.  
  76. void
  77. InitColor()
  78. {
  79.   int i;
  80.  
  81.   for (i=0; i<LUM_RANGE; i++) {
  82.     lum_values[i] = ((i * 256) / (LUM_RANGE)) + (256/(LUM_RANGE*2));
  83.   }
  84.  
  85.   for (i=0; i<CR_RANGE; i++) {
  86.     cr_values[i] = ((i * 256) / (CR_RANGE)) + (256/(CR_RANGE*2));
  87.   }
  88.  
  89.   for (i=0; i<CB_RANGE; i++) {
  90.     cb_values[i] = ((i * 256) / (CB_RANGE)) + (256/(CB_RANGE*2));
  91.   }
  92.  
  93. }
  94.  
  95.  
  96. /*
  97.  *--------------------------------------------------------------
  98.  *
  99.  * ConvertColor --
  100.  *
  101.  *    Given a l, cr, cb tuple, converts it to r,g,b.
  102.  *
  103.  * Results:
  104.  *    r,g,b values returned in pointers passed as parameters.
  105.  *
  106.  * Side effects:
  107.  *      None.
  108.  *
  109.  *--------------------------------------------------------------
  110.  */
  111.  
  112. static void
  113. ConvertColor(l, cr, cb, r, g, b)
  114.      unsigned char l, cr, cb;
  115.      unsigned char *r, *g, *b;
  116. {
  117.   double fl, fcr, fcb, fr, fg, fb;
  118.  
  119.   fl = (double) l;
  120.   fcr =  ((double) cr) - 128.0;
  121.   fcb =  ((double) cb) - 128.0;
  122.  
  123.  
  124.   fr = fl + (1.40200 * fcb);
  125.   fg = fl - (0.71414 * fcb) - (0.34414 * fcr);
  126.   fb = fl + (1.77200 * fcr);
  127.  
  128.   if (fr < 0.0) fr = 0.0;
  129.   else if (fr > 255.0) fr = 255.0;
  130.  
  131.   if (fg < 0.0) fg = 0.0;
  132.   else if (fg > 255.0) fg = 255.0;
  133.  
  134.   if (fb < 0.0) fb = 0.0;
  135.   else if (fb > 255.0) fb = 255.0;
  136.  
  137.   *r = (unsigned char) fr;
  138.   *g = (unsigned char) fg;
  139.   *b = (unsigned char) fb;
  140.  
  141. }
  142.  
  143. #ifdef SH_MEM
  144.  
  145. int gXErrorFlag = 0;
  146.  
  147. int HandleXError(dpy, event)
  148.      Display *dpy;
  149.      XErrorEvent *event;
  150. {
  151.   gXErrorFlag = 1;
  152.  
  153.   return 0;
  154. }
  155.  
  156. void InstallXErrorHandler()
  157. {
  158.   int HandleXError();
  159.  
  160.   XSetErrorHandler(HandleXError);
  161.   XFlush(display);
  162. }
  163.  
  164. void DeInstallXErrorHandler()
  165. {
  166.   XSetErrorHandler(NULL);
  167.   XFlush(display);
  168. }
  169. #endif
  170.  
  171.  
  172. /*
  173.  *--------------------------------------------------------------
  174.  *
  175.  * ResizeDisplay --
  176.  *
  177.  *    Resizes display window.
  178.  *
  179.  * Results:
  180.  *    None.
  181.  *
  182.  * Side effects:
  183.  *      None.
  184.  *
  185.  *--------------------------------------------------------------
  186.  */
  187.  
  188. void ResizeDisplay(w, h)
  189.      int w, h;
  190. {
  191.  
  192.   if (ditherType == NO_DITHER) return;
  193.  
  194.   XResizeWindow(display, window, w, h);
  195.   XFlush(display);
  196. }
  197.  
  198.  
  199. /*
  200.  *--------------------------------------------------------------
  201.  *
  202.  * MakeWindow --
  203.  *
  204.  *    Create X Window
  205.  *
  206.  * Results:
  207.  *    Read the code.
  208.  *
  209.  * Side effects:
  210.  *      None.
  211.  *
  212.  *--------------------------------------------------------------
  213.  */
  214.  
  215. #ifdef SH_MEM
  216. int CompletionType = -1;
  217. #endif
  218.  
  219. static void 
  220. MakeWindow(name) 
  221. char *name;
  222. {
  223.   
  224.   XSizeHints hint;
  225.   unsigned int fg, bg;
  226.   char *hello = "MPEG Play";
  227.   int screen;
  228.   Window CreateFullColorWindow();
  229.  
  230.   if (ditherType == NO_DITHER) return;
  231.  
  232.   display = XOpenDisplay(name);
  233.   if (display == NULL) {
  234.     fprintf(stderr, "Can not open display\n");
  235.     exit(-2);
  236.   }
  237.  
  238. #ifdef SH_MEM
  239.   if(shmemFlag)
  240.     CompletionType = XShmGetEventBase(display) + ShmCompletion;
  241. #endif
  242.  
  243.   screen = DefaultScreen (display);
  244.   
  245.   /* Fill in hint structure */
  246.  
  247.   hint.x = 200;
  248.   hint.y = 300;
  249.   hint.width = 150;
  250.   hint.height = 150;
  251.   hint.flags = PPosition | PSize;
  252.   
  253.   /* Get some colors */
  254.   
  255.   bg = WhitePixel (display, screen);
  256.   fg = BlackPixel (display, screen);
  257.   
  258.   /* Make the window */
  259.   
  260.   if (ditherType == FULL_COLOR_DITHER) {
  261.     window = CreateFullColorWindow (display, hint.x, hint.y, hint.width, hint.height);
  262.     if (window == 0) {
  263.       fprintf (stderr, "-color option only valid on full color display\n");
  264.       exit (-1);
  265.     }
  266.   } else if (ditherType == MONO_DITHER || ditherType == MONO_THRESHOLD) {
  267.     window = XCreateSimpleWindow (display,
  268.                   DefaultRootWindow (display),
  269.                   hint.x, hint.y,
  270.                   hint.width, hint.height,
  271.                   4, fg, bg);
  272.   } else {
  273.     XVisualInfo vinfo;
  274.     
  275.     if (!XMatchVisualInfo (display, screen, 8, PseudoColor, 
  276.                &vinfo)) {
  277.  
  278.       if (!XMatchVisualInfo(display, screen, 8, GrayScale, 
  279.                 &vinfo)) {
  280.  
  281.     fprintf(stderr, "-requires 8 bit display\n");
  282.     exit(-1);
  283.       }
  284.     }
  285.  
  286.     window = XCreateSimpleWindow (display,
  287.                  DefaultRootWindow (display),
  288.                  hint.x, hint.y,
  289.                  hint.width, hint.height,
  290.                  4, fg, bg);
  291.   }
  292.  
  293. /* @@@ AK, Won't bother with these bits of XWindows */
  294. #if !defined(OS2)  
  295.   XSelectInput(display, window, StructureNotifyMask);
  296.  
  297.   /* Tell other applications about this window */
  298.   
  299.   XSetStandardProperties (display, window, hello, hello, None, NULL, 0, &hint);
  300.   
  301.   /* Map window. */
  302.  
  303.   XMapWindow(display, window);
  304.  
  305.   /* Wait for map. */
  306.   while(1) {
  307.     XEvent    xev;
  308.  
  309.     XNextEvent(display, &xev);
  310.     if(xev.type == MapNotify && xev.xmap.event == window)
  311.       break;
  312.   }
  313.   XSelectInput(display, window, NoEventMask);
  314. #endif
  315. }
  316.   
  317.  
  318. /*
  319.  *--------------------------------------------------------------
  320.  *
  321.  * InitDisplay --
  322.  *
  323.  *    Initialized display, sets up colormap, etc.
  324.  *
  325.  * Results:
  326.  *      None.
  327.  *
  328.  * Side effects:
  329.  *      None.
  330.  *
  331.  *--------------------------------------------------------------
  332.  */
  333.  
  334. void InitDisplay(name)
  335. char *name;
  336. {
  337.  
  338.   int ncolors = LUM_RANGE*CB_RANGE*CR_RANGE;
  339.   XColor xcolor;
  340.   int i, lum_num, cr_num, cb_num;
  341.   unsigned char r, g, b;
  342.   Colormap dcmap;
  343.  
  344.   if (ditherType == NO_DITHER) return;
  345.  
  346.   MakeWindow(name);
  347.  
  348.   gc = XCreateGC(display, window, 0, 0);
  349.  
  350.   dcmap = cmap = XDefaultColormap(display, DefaultScreen(display));
  351.  
  352.   xcolor.flags = DoRed | DoGreen | DoBlue;
  353.  
  354.   retry_alloc_colors:
  355.   for (i=0; i<ncolors; i++) {
  356.  
  357.     lum_num = (i / (CR_RANGE*CB_RANGE))%LUM_RANGE;
  358.     cr_num = (i / CB_RANGE)%CR_RANGE;
  359.     cb_num = i % CB_RANGE;
  360.  
  361.     ConvertColor(lum_values[lum_num], cr_values[cr_num], cb_values[cb_num], &r, &g, &b);
  362.  
  363.     xcolor.red = r * 256;
  364.     xcolor.green = g * 256;
  365.     xcolor.blue = b * 256;
  366.  
  367.     if(XAllocColor(display, cmap, &xcolor) == 0 && cmap == dcmap) {
  368.       int j;
  369.       long tmp_pixel;
  370.       XWindowAttributes xwa;
  371.  
  372.       if (!quietFlag) {
  373.     fprintf(stderr, "Using private colormap.\n");
  374.       }
  375.  
  376.       /* Free colors. */
  377.       for(j = 0; j < i; j ++) {
  378.     tmp_pixel = pixel[j];
  379.         XFreeColors(display, cmap, &tmp_pixel, 1, 0);
  380.       }
  381.  
  382.       XGetWindowAttributes(display, window, &xwa);
  383.       cmap = XCreateColormap(display, window, xwa.visual, AllocNone);
  384.       XSetWindowColormap(display, window, cmap);
  385.  
  386.       goto retry_alloc_colors;
  387.     }
  388.     pixel[i] = xcolor.pixel;
  389.   }
  390.  
  391.   ximage = NULL;
  392. }
  393.  
  394.  
  395. /*
  396.  *--------------------------------------------------------------
  397.  *
  398.  * InitGrayDisplay --
  399.  *
  400.  *    Initialized display for gray scale dither.
  401.  *
  402.  * Results:
  403.  *      None.
  404.  *
  405.  * Side effects:
  406.  *      None.
  407.  *
  408.  *--------------------------------------------------------------
  409.  */
  410.  
  411. #define NUM_COLORS 128
  412.  
  413. void InitGrayDisplay(name)
  414. char *name;
  415. {
  416.   int ncolors = NUM_COLORS;
  417.   XColor xcolor;
  418.   int i;
  419.   Colormap dcmap;
  420.  
  421.   MakeWindow(name);
  422.  
  423.   gc = XCreateGC(display, window, 0, 0);
  424.  
  425.   dcmap = cmap = XDefaultColormap(display, DefaultScreen(display));
  426.  
  427.   xcolor.flags = DoRed | DoGreen | DoBlue;
  428.  
  429.   retry_alloc_grays:
  430.   for (i=0; i<ncolors; i++) {
  431.  
  432.     xcolor.red = (i*2) * 256;
  433.     xcolor.green = (i*2) * 256;
  434.     xcolor.blue = (i*2) * 256;
  435.  
  436.     if(XAllocColor(display, cmap, &xcolor) == 0 && cmap == dcmap) {
  437.       int j;
  438.       long tmp_pixel;
  439.       XWindowAttributes xwa;
  440.  
  441.       if (!quietFlag) {
  442.     fprintf(stderr, "Using private colormap.\n");
  443.       }
  444.  
  445.       /* Free colors. */
  446.       for(j = 0; j < i; j ++) {
  447.     tmp_pixel = pixel[j*2];
  448.         XFreeColors(display, cmap, &tmp_pixel, 1, 0);
  449.       }
  450.  
  451.       XGetWindowAttributes(display, window, &xwa);
  452.       cmap = XCreateColormap(display, window, xwa.visual, AllocNone);
  453.       XSetWindowColormap(display, window, cmap);
  454.  
  455.       goto retry_alloc_grays;
  456.     }
  457.     pixel[(i*2)] = xcolor.pixel;
  458.     pixel[(i*2)+1] = xcolor.pixel;
  459.   }
  460.  
  461.   ximage = NULL;
  462. }
  463.  
  464.  
  465. /*
  466.  *--------------------------------------------------------------
  467.  *
  468.  * InitMonoDisplay --
  469.  *
  470.  *    Initialized display for monochrome dither.
  471.  *
  472.  * Results:
  473.  *      None.
  474.  *
  475.  * Side effects:
  476.  *      None.
  477.  *
  478.  *--------------------------------------------------------------
  479.  */
  480.  
  481. void InitMonoDisplay(name)
  482. char *name;
  483. {
  484.   XGCValues xgcv;
  485.  
  486.   MakeWindow(name);
  487.  
  488.   xgcv.background = BlackPixel(display, DefaultScreen(display));
  489.   xgcv.foreground = WhitePixel(display, DefaultScreen(display));
  490.  
  491.   gc = XCreateGC(display, window, GCForeground | GCBackground, &xgcv);
  492.  
  493.   ximage = NULL;
  494. }
  495.  
  496.  
  497. /*
  498.  *--------------------------------------------------------------
  499.  *
  500.  * InitColorDisplay --
  501.  *
  502.  *    Initialized display for full color output.
  503.  *
  504.  * Results:
  505.  *      None.
  506.  *
  507.  * Side effects:
  508.  *      None.
  509.  *
  510.  *--------------------------------------------------------------
  511.  */
  512.  
  513. void InitColorDisplay(name)
  514. char *name;
  515. {
  516.  
  517.   MakeWindow(name);
  518.  
  519.   gc = XCreateGC(display, window, 0, 0);
  520.   ximage = NULL;
  521. }
  522.  
  523.  
  524. /*
  525.  *--------------------------------------------------------------
  526.  *
  527.  * ExecuteDisplay --
  528.  *
  529.  *    Actually displays display plane in previously created window.
  530.  *
  531.  * Results:
  532.  *    None.
  533.  *
  534.  * Side effects:
  535.  *    None.
  536.  *
  537.  *--------------------------------------------------------------
  538.  */
  539.  
  540. void
  541. ExecuteDisplay(vid_stream)
  542.      VidStream *vid_stream;
  543. {
  544.   char dummy;
  545.   Visual *FindFullColorVisual();
  546.   Visual *fc_visual;
  547.   int depth;
  548.  
  549.   totNumFrames++;
  550.   if (!quietFlag) {
  551.     fprintf (stderr, "%d\r", totNumFrames);
  552.   }
  553.  
  554.   if (ditherType == NO_DITHER) return;
  555.  
  556.   if (ximage == NULL) {
  557.  
  558.     if (ditherType == Twox2_DITHER) {
  559.       ximage = XCreateImage(display, None, 8, ZPixmap, 0, &dummy,
  560.                 vid_stream->mb_width * 32,
  561.                 vid_stream->mb_height * 32, 8, 0);
  562.     } else if (ditherType == FULL_COLOR_DITHER) {
  563.       fc_visual = FindFullColorVisual(display, &depth);
  564.       ximage = XCreateImage (display, fc_visual, depth, ZPixmap,
  565.                  0, &dummy, vid_stream->mb_width * 16,
  566.                  vid_stream->mb_height * 16, 32, 0);
  567.     } else if (ditherType == MONO_DITHER || ditherType == MONO_THRESHOLD) {
  568.       ximage = XCreateImage (display, None, 1, XYBitmap, 0, &dummy,
  569.                  vid_stream->mb_width * 16,
  570.                  vid_stream->mb_height * 16, 8, 0);
  571.       ximage->byte_order = MSBFirst;
  572.       ximage->bitmap_bit_order = MSBFirst;
  573.     } else {
  574.       ximage = XCreateImage(display, None, 8, ZPixmap, 0, &dummy,
  575.                 vid_stream->mb_width * 16,
  576.                 vid_stream->mb_height * 16, 8, 0);
  577.     }
  578.   }
  579.  
  580.   if (!noDisplayFlag) {
  581. #ifdef SH_MEM
  582.     if (shmemFlag) {
  583.       XShmPutImage(display, window, gc, vid_stream->current->ximage, 
  584.            0, 0, 0, 0,
  585.            vid_stream->current->ximage->width, 
  586.            vid_stream->current->ximage->height, True);
  587.       XFlush(display);
  588.       
  589.       while(1) {
  590.     XEvent xev;
  591.     
  592.     XNextEvent(display, &xev);
  593.     if(xev.type == CompletionType)
  594.       break;
  595.       }
  596.     }
  597.     else 
  598. #endif
  599.       
  600.       {
  601.     ximage->data = (char *) vid_stream->current->display; 
  602.     
  603.     XPutImage(display, window, gc, ximage, 0, 0, 0, 0, ximage->width, ximage->height);
  604.       }
  605.   }
  606. }
  607.  
  608.  
  609.